package cn.elwy.rcp.ui.table.setting.listener;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;

import cn.elwy.rcp.ui.table.TablePane;
import cn.elwy.rcp.ui.table.listener.TablePaneConfigListener;
import cn.elwy.rcp.ui.table.model.ColumnInfo;

public class ColumnSettingMenuDetectListener implements MenuDetectListener {

  private final TablePane tablePane;

  public ColumnSettingMenuDetectListener(TablePane tablePane) {
    this.tablePane = tablePane;
  }

  @Override
  public void menuDetected(MenuDetectEvent e) {
    // Point cursorLocation = e.display.getCursorLocation();
    Composite control = (Composite) e.widget;
    boolean isHeader = false;
    Point pt = control.getDisplay().map(null, control, e.x, e.y);
    Rectangle clientArea = control.getClientArea();
    int headerHeight = 0;
    if (control instanceof Tree) {
      Tree tree = (Tree) control;
      headerHeight = tree.getHeaderHeight();
    } else {
      Table table = (Table) control;
      headerHeight = table.getHeaderHeight();
    }
    isHeader = ((pt.y - clientArea.y) <= headerHeight);

    if (isHeader && tablePane.getColumnOrder().length > 1) {
      Menu columnSettingMenu = new Menu(Display.getDefault().getActiveShell(), SWT.POP_UP);
      control.setMenu(columnSettingMenu);

      createOpenColumnSettingDialogMenuItem(control, columnSettingMenu);
      new MenuItem(columnSettingMenu, SWT.SEPARATOR).setEnabled(true);
      createColumnVisibleSettingMenuItems(columnSettingMenu);
    } else {
      control.setMenu(tablePane.getContextMenu());
    }
  }

  private void createOpenColumnSettingDialogMenuItem(Control tree, Menu columnSettingMenu) {
    MenuItem menuItem = new MenuItem(columnSettingMenu, SWT.PUSH);
    menuItem.setText("列配置...");
    TablePaneConfigListener listener = new TablePaneConfigListener();
    listener.setTablePane(tablePane);
    menuItem.addSelectionListener(listener);
  }

  private void createColumnVisibleSettingMenuItems(Menu columnSettingMenu) {
    int[] columnOrder = tablePane.getColumnOrder();
    VisibleSettingListener listener = new VisibleSettingListener();
    listener.setTablePane(tablePane);
    List<ColumnInfo> columnInfos = tablePane.getTableInfo().getColumnInfoList();
    Map<String, Item> treeColumnMap = tablePane.getTreeColumnMap();
    for (int i = 0; i < columnInfos.size() && i < columnOrder.length; i++) {
      ColumnInfo columnInfo = columnInfos.get(columnOrder[i]);
      columnInfo.setOrder(i);
      MenuItem item = new MenuItem(columnSettingMenu, SWT.CHECK);
      item.setData("columnInfo", columnInfo);
      item.setText(columnInfo.getName());

      Item column = treeColumnMap.get(columnInfo.getId());
      if (column instanceof TableColumn) {
        TableColumn tableColumn = (TableColumn) column;
        item.setSelection(tableColumn.getResizable());
      } else {
        TreeColumn treeColumn = (TreeColumn) column;
        item.setSelection(treeColumn.getResizable());
      }
      item.setData(column);
      item.addSelectionListener(listener);
    }
  }
}
